﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StudentManage.Data;
using StudentManage.Common;

namespace StudentManage.Web.Post
{
    public partial class PostList : SecurityPage
    {
        private void RepDataBing()
        {
            List<TypeInfo> NewsTypeList = TypeInfo.Where("ProjectTypeid=@0", Convert.ToInt32(Request.QueryString["ProjectTypeid"])).ToList();
            this.RepTypeList.DataSource = NewsTypeList;
            this.RepTypeList.DataBind();
        }

        private void RepPostBing()
        {
            string XYDM = Session["SelectedXYDM"].ToString();
            string strtitle = (this.txt_Search.Text.Trim() == "请输入查询的标题") ? "" : this.txt_Search.Text.Trim();
            var eps = DynamicLinqExpressions.True<Project>();
            eps = eps.And(p=>p.IsDel==false&&p.IsAudit==true);
            eps = eps.And(p => p.College == "0" || p.College == XYDM);
            eps = eps.And(p => p.Title.Contains(strtitle));
            eps = eps.And(p => p.ProjectTypeid == Convert.ToInt32(Request.QueryString["ProjectTypeid"]));
            if (Request.QueryString["TypeId"] != null)
            {
                eps = eps.And(p => p.Typeid == Convert.ToInt32(Request.QueryString["TypeId"]));
            }

            int RowCount = LinqUtility.LinqContext.Project.Where(eps).Count();
            this.AspNetPager1.RecordCount = RowCount;
            int PageSize = this.AspNetPager1.PageSize;
            int SkipCount = PageSize * (this.AspNetPager1.CurrentPageIndex - 1);

            List<Project> newslist = LinqUtility.LinqContext.Project.Where(eps).OrderByDescending(p => p.AddTime).Skip(SkipCount).Take(PageSize).ToList();
            this.RepPostList.DataSource = newslist;
            this.RepPostList.DataBind();
        }

        public string GetTypeName(string typeid)
        {
            return TypeInfo.Where("id=@0", int.Parse(typeid)).First().TypeName;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            UC_MasterDrop uc = Page.Controls.AllOfType<UC_MasterDrop>().FirstOrDefault();
            uc.SelectedChanging += new UC_MasterDrop.DropEventDelegate(RepPostBing);

            if (IsPostBack) return;
            RepDataBing();
            RepPostBing();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            this.RepPostBing();
        }

        protected void Btn_Search_Click(object sender, EventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = 1;
            this.RepPostBing();
        }
    }
}